import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'
import user from '@/api/user'
import common from '@/api/common'

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        token: null,
        user: null,
        replyItem: null,
        base_config: null
    },
    mutations: {
        setToken (state, token) {
            state.token = token
        },
        setUser (state, user) {
            state.user = user
        },
        setReplyItem (state, reply) {
            state.replyItem = reply
        },
        setBaseConfig (state, data) {
            state.base_config = data
        }
    },
    actions: {
        getUser (context) {
            user.info().then(({ code, data }) => {
                if (code === 200) {
                    context.commit('setUser', data)
                }
            })
        },
        baseConfig: ({ commit }) => {
            common.config('base').then(({ code, data }) => {
                if (code === 200) {
                    commit('setBaseConfig', data)
                }
            })
        }
    },
    getters: {
        userinfo: state => {
            return {
                truename: state.user.truename,
                qq: state.user.qq,
                avatar: state.user.avatar,
                nickname: state.user.nickname,
                desc: state.user.desc,
                sex: state.user.sex,
                birthday: state.user.birthday,
                refcode: state.user.refcode
            }
        },
        numberPlays: state => {
            return state.user ? state.user.share_free_num + state.user.vip_share_free_num : 0
        }
    },
    modules: {},
    plugins: [createPersistedState()]
})
